package com.ryan.member.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ryan.member.entity.Member;
import com.ryan.member.entity.vo.LoginVo;
import com.ryan.member.entity.vo.RegisterVo;
import com.ryan.member.entity.vo.UpdatePwdVo;
import com.ryan.member.service.MemberService;
import com.ryan.utils.JwtUtil;
import com.ryan.utils.R;
import com.ryan.utils.vo.OrderMember;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author ryan
 * @since 2020-07-29
 */
@RestController
@RequestMapping("/ucenter")
public class MemberController {

    @Autowired
    private MemberService memberService;

    /**
     * 注册功能
     * @param registerVo
     * @return
     */
    @PostMapping("/register")
    public R register(@RequestBody RegisterVo registerVo){
        int count = memberService.register(registerVo);
        return count==0? R.error():R.ok();
    }

    /**
     * 登陆功能，单点登陆，登陆成功后返回token
     * @param loginVo
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody LoginVo loginVo){
        String token = memberService.login(loginVo);
        return R.ok().data("token", token);
    }

    /**
     * 通过token获取用户信息
     * @param request
     * @return
     */
    @GetMapping("/getUserInfoByToken")
    public R getUserInfoByToken(HttpServletRequest request){
        System.out.println("通过token获取id前");
        String userId = JwtUtil.getMemberIdByJwtToken(request);
        System.out.println("通过token获取id后" + userId);
        Member member = memberService.getById(userId);
        return member==null?R.error():R.ok().data("userInfo", member);
    }

    /**
     * 通过id获取会员信息，主要用于生成订单时服务调用
     * @param id
     * @return
     */
    @GetMapping("/getOrderMemberById/{id}")
    public OrderMember getOrderMemberById(@PathVariable String id){
        Member member = memberService.getById(id);
        OrderMember orderMember = new OrderMember();
        BeanUtils.copyProperties(member, orderMember);
        return orderMember;
    }

    /**
     * 更新用户信息
     * @param member
     * @return
     */
    @PostMapping("/updateMember")
    public R updateMember(@RequestBody Member member){
        boolean update = memberService.updateById(member);
        return update?R.ok():R.error();
    }

    /**
     * 添加用户
     * @param member
     * @return
     */
    @PostMapping("/addMember")
    public R addMember(@RequestBody Member member){
        boolean save = memberService.save(member);
        return save?R.ok():R.error();
    }

    /**
     * 条件+分页查询所有的会员列表
     * @param pageIndex
     * @param pageSize
     * @param name
     * @return
     */
    @PostMapping("/getMemberList/{pageIndex}/{pageSize}")
    public R getMemberList(@PathVariable long pageIndex,
                           @PathVariable long pageSize,
                           @RequestBody(required = false) String name
    ){
        Page<Member> resultPage = memberService.getMemberList(pageIndex, pageSize, name);
        return R.ok().data("list", resultPage.getRecords()).data("totalPage", resultPage.getTotal());
    }

    /**
     * 删除会员
     * @param ids
     * @return
     */
    @PostMapping("/deleteMember")
    public R deleteMember(@RequestBody Long[] ids){
        boolean delete = memberService.removeByIds(Arrays.asList(ids));
        return delete?R.ok(): R.error();
    }

    /**
     * 通过id获取用户信息，主要用于用户中心
     * @param id
     * @return
     */
    @GetMapping("/getMemberInfoById/{id}")
    public R getMemberInfoById(@PathVariable String id){
        Member memberInfo = memberService.getById(id);
        return R.ok().data("memberInfo",memberInfo);
    }

    /**
     * 用户修改密码接口
     * @param updatePwdVo
     * @return
     */
    @PostMapping("/updatePwd")
    public R updatePwd(@RequestBody UpdatePwdVo updatePwdVo){
        int count = memberService.updatePwd(updatePwdVo);
        return count>0?R.ok():R.error();
    }

    /**
     * 统计某个时间的注册人数
     * @param day
     * @return
     */
    @GetMapping("/countRegister/{day}")
    public R countRegister(@PathVariable String day){
        Integer registerCount = memberService.countRegister(day);
        return R.ok().data("registerCount",registerCount);
    }

}

